package com.shitou.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shitou.dao.NewsDao;
import com.shitou.pojo.Assignment;
import com.shitou.pojo.Comment;
import com.shitou.pojo.Document;
import com.shitou.pojo.Issues;
import com.shitou.pojo.Milestone;
import com.shitou.pojo.News;
import com.shitou.pojo.Project;
import com.shitou.pojo.User;
import com.shitou.util.DateUtil;
import com.shitou.util.IssuesUtil;
import com.shitou.util.NewsUtil;

@Service
@Transactional
public class NewsService {

	@Resource
	private NewsDao newsDao;
	@Resource
	private IssuesService issuesService;
	@Resource
	private UserService userService;
	
	public void save(News news,User user) {
		news.setCreatedate(DateUtil.getNow());
		news.setType(NewsUtil.CHAT);
		news.setUser(user);
		StringBuilder content = new StringBuilder();
		content.append("<a href=\"user.jspx?id="+user.getId()+"\">"+user.getName()+"</a>说：");
		content.append("<blockquote>&gt;&nbsp;"+news.getContent()+"</blockquote>");
		news.setContent(content.toString());
		newsDao.save(news);
	}
	
	public void save(Comment comment, User user) {
		News news = new News();
		news.setCreatedate(DateUtil.getNow());
		news.setType(NewsUtil.COMMENTS);
		String title;
		if(comment.getIssues().getType() == IssuesUtil.TASK) {
			title = "任务";
		} else if(comment.getIssues().getType() == IssuesUtil.BUG) {
			title = "错误";
		} else {
			title = "想法";
		}
		int endIndex = comment.getContent().length() < 40 ? comment.getContent().length() : 40;
		StringBuilder content = new StringBuilder();
		content.append("<a href=\"user.jspx?id="+user.getId()+"\">"+user.getName()+"</a>评论了"+title+"'<a href=\"issues.jspx?id="+comment.getIssues().getId()+"\">"+comment.getIssues().getTitle()+"</a>'");
		content.append("<blockquote>&gt;&nbsp;"+comment.getContent().substring(0, endIndex)+"</blockquote>");
		news.setContent(content.toString());
		news.setUser(user);
		newsDao.save(news);
	}
	
	public void save(Milestone milestone, User user) {
		News news = new News();
		news.setCreatedate(DateUtil.getNow());
		news.setType(NewsUtil.MILESTONE);
		StringBuilder content = new StringBuilder();
		content.append("<a href=\"user.jspx?id="+user.getId()+"\">"+user.getName()+"</a>在项目<a href=\"project.jspx?id="+milestone.getProject().getId()+"\">"+milestone.getProject().getName()+"</a>新建了一个里程碑：<a href=\"document.jspx?id="+milestone.getId()+"\">"+milestone.getName()+"</a>");
		news.setContent(content.toString());
		news.setUser(user);
		newsDao.save(news);
	}
	
	public void save(Document document, User user) {
		News news = new News();
		news.setCreatedate(DateUtil.getNow());
		news.setType(NewsUtil.DOCUMENT);
		int endIndex = document.getContent().length() < 40 ? document.getContent().length() : 40;
		String title;
		if(document.getUpdatedate() != null) {
			title = "修改了";
		} else {
			title = "新建了";
		}
		StringBuilder content = new StringBuilder();
		content.append("<a href=\"user.jspx?id="+user.getId()+"\">"+user.getName()+"</a>"+title+"文档：<a href=\"document.jspx?id="+document.getId()+"\">"+document.getTitle()+"</a>");
		content.append("<blockquote>&gt;&nbsp;"+document.getContent().substring(0, endIndex)+"...</blockquote>");
		news.setContent(content.toString());
		news.setUser(user);
		newsDao.save(news);
	}
	
	public void save(Assignment assignment, int state, User user) {
		News news = new News();
		news.setCreatedate(DateUtil.getNow());
		news.setType(NewsUtil.SYSTEM);
		String title;
		if(assignment.getIssues().getType() == IssuesUtil.TASK) {
			title = "任务";
		} else if(assignment.getIssues().getType() == IssuesUtil.BUG) {
			title = "错误";
		} else {
			title = "想法";
		}
		StringBuilder content = new StringBuilder();
		content.append("<a href=\"user.jspx?id="+user.getId()+"\">"+user.getName()+"</a>把"+title+"'<a href=\"issues.jspx?id="+assignment.getIssues().getId()+"\">"+assignment.getIssues().getTitle()+"</a>'");
		content.append("的状态修改为<span><img width=\"50\" height=\"17\" class=\"issues-state-image\" src=\"images/issues-state-"+state+".png\" /></span>");
		news.setContent(content.toString());
		news.setUser(user);
		newsDao.save(news);
	}
	
	public void save(Assignment assignment, User user) {
		News news = new News();
		news.setCreatedate(DateUtil.getNow());
		news.setType(NewsUtil.KEY);
		String title;
		Issues issues = issuesService.findById(assignment.getIssues().getId());
		User user2 = userService.findById(assignment.getUser().getId()); 
		if(issues.getType() == IssuesUtil.TASK) {
			title = "任务";
		} else if(issues.getType() == IssuesUtil.BUG) {
			title = "错误";
		} else {
			title = "想法";
		}
		
		StringBuilder content = new StringBuilder();
		content.append("<a href=\"user.jspx?id="+user.getId()+"\">"+user.getName()+"</a>把"+title+"'<a href=\"issues.jspx?id="+issues.getId()+"\">"+issues.getTitle()+"</a>'分配给了<a href=\"user.jspx?id="+user2.getId()+"\">"+user2.getName()+"</a>");
		news.setContent(content.toString());
		news.setUser(user);
		newsDao.save(news);
	}
	
	public void save(Issues issues) {
		News news = new News();
		news.setCreatedate(DateUtil.getNow());
		news.setType(NewsUtil.TASK);
		String title;
		if(issues.getType() == IssuesUtil.TASK) {
			title = "创建了一个任务：";
		} else if(issues.getType() == IssuesUtil.BUG){
			title = "报告了一个错误：";
		} else {
			title = "添加了一个想法";
		}
		int endIndex = issues.getContent().length() < 40 ? issues.getContent().length() : 40;
		StringBuilder content = new StringBuilder();
		content.append("<a href=\"user.jspx?id="+issues.getUser().getId()+"\">"+issues.getUser().getName()+"</a>在项目<a href=\"project.jspx?id="+issues.getProject().getId()+"\">"+issues.getProject().getName()+"</a>中"+title+"<a href=\"issues.jspx?id="+issues.getId()+"\">"+issues.getTitle()+"</a>");
		content.append("<blockquote>&gt;&nbsp;"+issues.getContent().substring(0, endIndex)+"...</blockquote>");
		news.setContent(content.toString());
		news.setUser(issues.getUser());
		newsDao.save(news);
	}
	
	public void save(Project project, User user) {
		News news = new News();
		news.setCreatedate(DateUtil.getNow());
		news.setType(NewsUtil.PROJECT);
		StringBuilder content = new StringBuilder();
		content.append("<a href=\"user.jspx?id="+user.getId()+"\">"+user.getName()+"</a>创建了一个项目<a href=\"$project.jspx?id="+project.getId()+"\">"+project.getName()+"</a>");
		news.setContent(content.toString());
		news.setUser(user);
		newsDao.save(news);
	}
	
	public News findById(int id) {
		return newsDao.findById(id);
	}
	
	public List<News> findAll() {
		return newsDao.findAll();
	}
	
	public List<News> findLastTen() {
		return newsDao.findAll(0, 10, false, "id");
	}
	
	
}
